<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/9
 * Time: 10:14
 */

require_once '../DbTools.php';

$usercard = $_POST['usercard'];
$userpwd = $_POST['userpwd'];
//连接数据库
$conn = new mysqli();
//设置参数
$conn->connect('127.0.0.1','root','123456','bank');
if(!empty($conn->connect_error)){
    echo '数据库连接错误'.$conn->connect_error;
    die;
}
echo '数据库连接成功','<br />';

DbTools::setConn($conn);
$data = DbTools::select('select accountinfo.RealName, bankcard.CardNo , bankcard.CardPwd from accountinfo
inner join bankcard
on accountinfo.AccountId = bankcard.AccountId
where bankcard.CardNo ='.$usercard);
//print_r($data);
$aa = false;
$username = '-1';
for($i=0;$i<count($data);$i++){
    $CardNo = $data[$i]['CardNo'];
    $CardPwd = $data[$i]['CardPwd'];
    if($usercard == $CardNo && $userpwd == $CardPwd) {
        $aa = true;
        $username = $data[$i]['RealName'];
    }
}
if($aa == true){
    echo '登录成功','<br />';
}else{
    echo '账号或密码错误';
    echo '<a href="../login.php">重新登录</a>';
    die;
}

$userkey = md5($usercard.$userpwd);
setcookie('userkey',$userkey,time()+24*3600,'/');
setcookie('username',$username,time()+24+3600,'/');
session_start();
$_SESSION[$userkey] = [
    'usercard' => $usercard,
    'userpwd' => $userpwd
];

echo '<a href="userinfo.php">个人主页</a>';
//$username = '123';
//echo $usercard;
//$username = DbTools::select(
//    'select a.RealName from accountinfo a
//inner join bankcard b
//on a.AccountId = b.AccountId
//where b.CardNo = '.$usercard.'
//group by a.AccountId');
//
//print_r($username);

?>